package com.toulouseculture.controller;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import com.google.android.maps.GeoPoint;
import com.toulouseculture.bo.CinemaPlace;
import com.toulouseculture.bo.CulturePlace;
import com.toulouseculture.bo.LibraryPlace;
import com.toulouseculture.bo.MuseumPlace;
import com.toulouseculture.bo.TheaterPlace;

public class CulturePlaceController {

	public final static String CULTURE_PLACE_ID = "id";

	private static CulturePlaceController instance;

	private final List<MuseumPlace> mMuseumPlace = new ArrayList<MuseumPlace>();

	private final List<CinemaPlace> mCinemaPlace = new ArrayList<CinemaPlace>();

	private final List<LibraryPlace> mLibraryPlace = new ArrayList<LibraryPlace>();

	private final List<TheaterPlace> mTheaterPlace = new ArrayList<TheaterPlace>();

	private boolean cinemaPopulated = false;

	private boolean theaterPopulated = false;

	private boolean libraryPopulated = false;

	private boolean museumPopulated = false;

	protected CulturePlaceController() {
		super();
	}

	public static CulturePlaceController getInstance() {
		if (instance == null) {
			instance = new CulturePlaceController();
		}

		return instance;
	}

	/**
	 * Populate the cinema list from cvs file
	 * 
	 * @param inputStream
	 *            csv file containing data
	 */
	public void populateCinema(InputStream inputStream) {

		if (!cinemaPopulated) {

			try {

				// create BufferedReader to read csv file
				BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
				String strLine = "";
				int lineNumber = 0;

				// read comma separated file line by line
				while ((strLine = br.readLine()) != null) {
					if (lineNumber > 0) {

						// process the line, stuff to List
						String[] values = strLine.split(";");

						CinemaPlace cinema = new CinemaPlace();

						populateCulturePlacePart(CulturePlace.CINEMA_ID, cinema, values);

						cinema.setUrlProgramme(values[8]);
						cinema.setAllocineCode(values[9]);

						mCinemaPlace.add(cinema);

					}
					lineNumber++;

				}

			} catch (Exception e) {
				System.out.println("Exception while reading csv file: " + e);
			}
		}

		cinemaPopulated = true;
	}

	public void populateMuseum(InputStream inputStream) {

		if (!museumPopulated) {

			try {

				// create BufferedReader to read csv file
				BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
				String strLine = "";
				int lineNumber = 0;

				// read comma separated file line by line
				while ((strLine = br.readLine()) != null) {
					if (lineNumber > 0) {

						// process the line, stuff to List
						String[] values = strLine.split(";");

						MuseumPlace museum = new MuseumPlace();

						populateCulturePlacePart(CulturePlace.MUSEUM_ID, museum, values);

						mMuseumPlace.add(museum);

					}
					lineNumber++;

				}

			} catch (Exception e) {
				System.out.println("Exception while reading csv file: " + e);
			}
		}
		museumPopulated = true;
	}

	public void populateLibrary(InputStream inputStream) {

		if (!libraryPopulated) {
			try {

				// create BufferedReader to read csv file
				BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
				String strLine = "";
				int lineNumber = 0;

				// read comma separated file line by line
				while ((strLine = br.readLine()) != null) {
					if (lineNumber > 0) {

						// process the line, stuff to List
						String[] values = strLine.split(";");

						LibraryPlace library = new LibraryPlace();

						populateCulturePlacePart(CulturePlace.LIBRARY_ID, library, values);

						library.setDescription(values[8]);
						library.setHoraire(values[9]);
						library.setDirection(values[10]);
						// library.setLibraryType(values[11].equals("Bibliotheque")
						// ? LibraryType.LIBRARY
						// : values[11].equals("Bibliobus") ?
						// LibraryType.BUS_LIBRARY : null);
						library.setLibraryType(values[11]);

						mLibraryPlace.add(library);

					}
					lineNumber++;

				}

			} catch (Exception e) {
				System.out.println("Exception while reading csv file: " + e);
			}
		}

		libraryPopulated = true;
	}

	public void populateTheater(InputStream inputStream) {

		if (!theaterPopulated) {

			try {

				// create BufferedReader to read csv file
				BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
				String strLine = "";
				int lineNumber = 0;

				// read comma separated file line by line
				while ((strLine = br.readLine()) != null) {
					if (lineNumber > 0) {

						// process the line, stuff to List
						String[] values = strLine.split(";");

						TheaterPlace theater = new TheaterPlace();

						populateCulturePlacePart(CulturePlace.THEATER_ID, theater, values);

						theater.setType(values[8]);
						theater.setDescription(values[9]);

						mTheaterPlace.add(theater);

					}
					lineNumber++;

				}

			} catch (Exception e) {
				System.out.println("Exception while reading csv file: " + e);
			}
		}
		theaterPopulated = true;
	}

	protected void populateCulturePlacePart(Integer typeId, CulturePlace culturePlace, String[] values) {

		culturePlace.setId(typeId + Integer.parseInt(values[0]));
		culturePlace.setName(values[1]);
		culturePlace.setNoStreet(values[2]);
		culturePlace.setAddress(values[3]);
		culturePlace.setUrl(values[4]);
		culturePlace.setPhone(values[5]);
		culturePlace.setGeoPoint(newGeoPoint(Float.parseFloat(values[7].replace(',', '.')), Float.parseFloat(values[6].replace(',', '.'))));

	}

	public static GeoPoint newGeoPoint(float lat, float lng) {
		return new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
	}

	public List<CulturePlace> getAllCulturePlace() {
		List<CulturePlace> culturePlaces = new ArrayList<CulturePlace>();

		culturePlaces.addAll(mCinemaPlace);
		culturePlaces.addAll(mLibraryPlace);
		culturePlaces.addAll(mMuseumPlace);
		culturePlaces.addAll(mTheaterPlace);

		return culturePlaces;

	}

	public CulturePlace getCulturePlaceById(int id) {

		List<CulturePlace> culturePlaces = getAllCulturePlace();

		for (CulturePlace cp : culturePlaces) {
			if (cp.getId().equals(id)) {
				return cp;
			}
		}

		return null;
	}

	public List<CinemaPlace> getAllCinemaPlace() {
		return mCinemaPlace;
	}

	public List<MuseumPlace> getAllMuseumPlace() {
		return mMuseumPlace;
	}

	public List<LibraryPlace> getAllLibraryPlace() {
		return mLibraryPlace;
	}

	public List<TheaterPlace> getAllTheaterPlace() {
		return mTheaterPlace;
	}

}
